﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WeiXinSDK.DataModel;

namespace WeiXinDemo
{
    /// <summary>
    /// WeiXinHandler 的摘要说明
    /// </summary>
    public class WeiXinHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            WeiXinSDK.WeiXin.EventRequest(new EventAction());
        }

        public class EventAction : WeiXinSDK.IWeiXinEvent
        {

            public void AsyncProcess(RequestXML RequestXML)
            {
                WeiXinSDK.DataModel.AsyncMessage message = new WeiXinSDK.DataModel.AsyncMessage();
                message.touser = RequestXML.FromUserName;

                switch (RequestXML.RequestMsgType)
                {
                    case WeiXinSDK.DataModel.Enum.RequestMsgType.Text:
                        message.PassiveMessageType = WeiXinSDK.DataModel.Enum.PassiveMessageType.Text;
                        message.text = new text() { content = "helllo word" };
                        break;
                    case WeiXinSDK.DataModel.Enum.RequestMsgType.Location:
                        break;
                    case WeiXinSDK.DataModel.Enum.RequestMsgType.Image:
                        message.PassiveMessageType = WeiXinSDK.DataModel.Enum.PassiveMessageType.Text;
                        message.text = new text() { content = "你的图片我收下了" };
                        break;
                    case WeiXinSDK.DataModel.Enum.RequestMsgType.Voice:
                        break;
                    case WeiXinSDK.DataModel.Enum.RequestMsgType.Video:
                        message.PassiveMessageType = WeiXinSDK.DataModel.Enum.PassiveMessageType.Text;
                        message.text = new text() { content = "你真是个美男子" };
                        break;
                    case WeiXinSDK.DataModel.Enum.RequestMsgType.Link:
                        break;
                    case WeiXinSDK.DataModel.Enum.RequestMsgType.Event:
                        message.PassiveMessageType = WeiXinSDK.DataModel.Enum.PassiveMessageType.News;
                        List<articles> articles = new List<articles>() { 
                             new articles(){
                                title = "helllo word",
                                description = "helllo word",
                                picurl = "http://d.hiphotos.baidu.com/image/pic/item/8ad4b31c8701a18b2495484e9c2f07082838feb1.jpg",
                                url = "http://www.baidu.com"
                             }
                        };
                        message.news = new news()
                        {
                            articles = articles
                        };
                        break;
                }
                string resultMsg = string.Empty;
                WeiXinSDK.WeiXinAPI.SentServiceInfo(message, out resultMsg);
            }


            public Message SyncProcess(RequestXML RequestXML)
            {
                WeiXinSDK.DataModel.Message message = new WeiXinSDK.DataModel.Message();
                message.ToUserName = RequestXML.FromUserName;
                message.FromUserName = RequestXML.ToUserName;
                message.AddTime = DateTime.Now;

                switch (RequestXML.RequestMsgType)
                {
                    case WeiXinSDK.DataModel.Enum.RequestMsgType.Text:
                        message.PassiveMessageType = WeiXinSDK.DataModel.Enum.PassiveMessageType.Text;
                        message.Content = "helllo word";
                        break;
                    case WeiXinSDK.DataModel.Enum.RequestMsgType.Location:
                        break;
                    case WeiXinSDK.DataModel.Enum.RequestMsgType.Image:
                        message.PassiveMessageType = WeiXinSDK.DataModel.Enum.PassiveMessageType.Text;
                        message.Content = "你的图片我收下了";
                        break;
                    case WeiXinSDK.DataModel.Enum.RequestMsgType.Voice:
                        message = null;
                        break;
                    case WeiXinSDK.DataModel.Enum.RequestMsgType.Video:
                        message.PassiveMessageType = WeiXinSDK.DataModel.Enum.PassiveMessageType.Text;
                        message.Content = "你真是个美男子";
                        break;
                    case WeiXinSDK.DataModel.Enum.RequestMsgType.Link:
                        break;
                    case WeiXinSDK.DataModel.Enum.RequestMsgType.Event:
                        message.PassiveMessageType = WeiXinSDK.DataModel.Enum.PassiveMessageType.News;
                        message.ArticleCount = 1;

                        WeiXinSDK.DataModel.MessageContent.item Article = new WeiXinSDK.DataModel.MessageContent.item();
                        Article.Title = "helllo word";
                        Article.Description = "helllo word";
                        Article.Url = "http://www.baidu.com";
                        Article.PicUrl = "http://d.hiphotos.baidu.com/image/pic/item/8ad4b31c8701a18b2495484e9c2f07082838feb1.jpg";

                        message.Articles = new List<MessageContent.item>();
                        message.Articles.Add(Article);
                        break;
                }
                return message;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}